package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Creates the legal statement dialog box
 * 
 * @author Stephan
 * 
 */
@SuppressWarnings("serial")
public class LegalStatementDialog extends JDialog implements ActionListener
{
	/**
	 * A boolean indicating whether the user clicked accept or decline
	 */
	private boolean accepted;

	/**
	 * The accept button
	 */
	private JButton accept;
	/**
	 * The decline button
	 */
	private JButton decline;

	/**
	 * Creates a dialog that displays the legal statement
	 * 
	 * @param legalStatement
	 *            the file containing the legal statement
	 */
	public LegalStatementDialog(File legalStatement)
	{
		this.getContentPane().setLayout(new BorderLayout());

		JTextArea legalInfoPane = new JTextArea();
		legalInfoPane.setEditable(false);
		legalInfoPane.setWrapStyleWord(true);
		//legalInfoPane.setMaximumSize(new Dimension(400, Integer.MAX_VALUE));
		JScrollPane scrollPane = new JScrollPane(legalInfoPane);

		JLabel header = new JLabel("<html><h1>Terms of Use</h1>");

		accept = new JButton("Accept");
		decline = new JButton("Decline");

		accept.addActionListener(this);
		decline.addActionListener(this);

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(accept);
		buttonPanel.add(decline);

		Scanner fileIn = null;
		try
		{
			Properties props = new Properties();
			props.load(new FileInputStream("resources/config.cfg"));
			
			fileIn = new Scanner(new File(props.getProperty("LegalStatement")));
			fileIn.useDelimiter("\\z");
			legalInfoPane.setText(fileIn.next());
		}
		catch (IOException e)
		{
			JOptionPane.showMessageDialog(this,
					"There was an error retrieving the legal statement",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
		finally
		{
			if (fileIn != null)
				fileIn.close();
		}

		this.setModal(true);

		this.getContentPane().add(header, BorderLayout.PAGE_START);
		this.getContentPane().add(scrollPane, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
		
		this.pack();
		//this.setSize(new Dimension(this.getSize().width, 500));
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if (arg0.getSource().equals(accept))
		{
			accepted = true;
			dispose();
		}
		else if (arg0.getSource().equals(decline))
		{
			accepted = false;
			dispose();
		}

	}

	/**
	 * Gets whether the user accepted or declined the legal statement
	 * 
	 * @return <code>true</code> if the user accepted, <code>false</code>
	 *         otherwise
	 */
	public boolean showModalDialog()
	{
		this.setVisible(true);
		return accepted;
	}
}
